package com.mxpio.erp.quality.service.impl;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.inventory.entity.WarehouseOrder;
import com.mxpio.erp.common.inventory.entity.WarehouseOrderLine;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.quality.entity.ScrapOutApply;
import com.mxpio.erp.common.quality.entity.ScrapOutApplyLine;
import com.mxpio.erp.common.quality.entity.ScrapOutNotice;
import com.mxpio.erp.common.quality.entity.ScrapOutNoticeLine;
import com.mxpio.erp.common.quality.service.ScrapOutApplyService;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.vo.OrderExcuteLineVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ScrapOutApplyServiceImpl extends BaseServiceImpl<ScrapOutApply> implements ScrapOutApplyService {
    @Override
    @Transactional
    public Result<? extends Order> agreeByBizNo(String bizNo) {
        ScrapOutApply po = this.getById(ScrapOutApply.class, bizNo);
        List<ScrapOutApplyLine> lineList = JpaUtil.linq(ScrapOutApplyLine.class).equal("bizNo", po.getBizNo()).list();
        if (lineList.size()==0){
            return Result.error("报废出库申请明细为空，无法生成对应的通知单和仓单！");
        }
        if (po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode())) {
            Set<String> itemCodeSet = lineList.stream().map(ScrapOutApplyLine::getItemCode).collect(Collectors.toSet());
            List<Item> itemList = JpaUtil.linq(Item.class).in("itemCode", itemCodeSet).list();
            Map<String, Item> itemMap = JpaUtil.index(itemList,"itemCode");//查询物料
            //自动生成通知单和仓单
            List<OrderExcuteLineVo> noticeVos = new ArrayList<>();
            for (ScrapOutApplyLine line:lineList){
                OrderExcuteLineVo excuteLineVo = new OrderExcuteLineVo();
                excuteLineVo.setBizOrderNo(po.getBizNo());
                excuteLineVo.setBizOrderlineNo(line.getLineNo());
                excuteLineVo.setItemCode(line.getItemCode());
                excuteLineVo.setLineQuantity(line.getQuantity());
                excuteLineVo.setLotNo(line.getLotNo());
                excuteLineVo.setSubLotIndex(line.getSubLotIndex());
                excuteLineVo.setQuantity(line.getQuantity());
                excuteLineVo.setWhCode(po.getWhCode());
                excuteLineVo.setMemo(StringUtils.isBlank(line.getMemo())?line.getScrapReason():line.getMemo());
                noticeVos.add(excuteLineVo);
            }
            Notice notice = null;
            int noticeLineNo = 0;
            List<OrderExcuteLineVo> woVos = new ArrayList<>();
            for(OrderExcuteLineVo orderExcuteVo : noticeVos) {
                if(notice == null) {//20221123 wpp 下边判断增加accessType条件，out类型的操作不需要生成QualityClass检验单
                    notice = new ScrapOutNotice();
                    notice.setBizOrderNo(po.getBizNo());
                    notice.setNoticeDate(new Date());
                    notice.setWhCode(orderExcuteVo.getWhCode());
                    notice.setAccessType("out");
                    notice.setPickingType(NoticeStatusEnums.NoticeType.SOA_OUT.getCode());//报废出库
                    notice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CONFIRM.getStatusCode());//直接确认状态
                    JpaUtil.save(notice);
                }
                noticeLineNo++;
                ScrapOutNoticeLine noticeLine = new ScrapOutNoticeLine();
                noticeLine.setOriginBizLineNo(orderExcuteVo.getBizOrderlineNo());
                noticeLine.setOriginBizNo(orderExcuteVo.getBizOrderNo());
                noticeLine.setItemCode(orderExcuteVo.getItemCode());
                noticeLine.setNoticeNo(notice.getNoticeNo());
                noticeLine.setQuantity(orderExcuteVo.getQuantity());
                noticeLine.setSort(noticeLineNo+"");
                noticeLine.setLotNo(orderExcuteVo.getLotNo());
                noticeLine.setSubLotIndex(orderExcuteVo.getSubLotIndex());
                noticeLine.setLineNo(noticeLineNo+"");
                noticeLine.setMemo(orderExcuteVo.getMemo());
                Item item = itemMap.get(orderExcuteVo.getItemCode());
                if (item!=null){
                    noticeLine.setItemName(item.getItemName());
                    noticeLine.setItemSpec(item.getItemSpec());
                    noticeLine.setUnitCode(item.getUnitCode());
                    noticeLine.setDrawingNo(item.getDrawingNo());
                }
                JpaUtil.save(noticeLine);

                OrderExcuteLineVo excuteLineVo = new OrderExcuteLineVo();
                excuteLineVo.setBizOrderNo(orderExcuteVo.getBizOrderNo());
                excuteLineVo.setBizOrderlineNo(orderExcuteVo.getBizOrderlineNo());
                excuteLineVo.setItemCode(orderExcuteVo.getItemCode());
                excuteLineVo.setLineQuantity(orderExcuteVo.getQuantity());
                excuteLineVo.setLotNo(orderExcuteVo.getLotNo());
                excuteLineVo.setSubLotIndex(orderExcuteVo.getSubLotIndex());
                excuteLineVo.setQuantity(orderExcuteVo.getQuantity());
                excuteLineVo.setWhCode(orderExcuteVo.getWhCode());
                excuteLineVo.setMemo(orderExcuteVo.getMemo());
                excuteLineVo.setNoticeNo(noticeLine.getNoticeNo());
                excuteLineVo.setNoticeLineNo(noticeLine.getLineNo());
                woVos.add(excuteLineVo);
            }

            //自动生成仓单
            WarehouseOrder wo = null;
            int woLineNo = 0;
            for(OrderExcuteLineVo vo : woVos){
                woLineNo++;
                if(wo == null){
                    wo = new WarehouseOrder();
                    wo.setAccessType(notice.getAccessType());
                    wo.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
                    wo.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
                    wo.setOrderStatus(OrderStatusEnums.OrderStatus.PLAN.getStatusCode());
                    wo.setOrderDate(new Date());
                    wo.setOriginBizNo(vo.getBizOrderNo());
                    wo.setWhCode(vo.getWhCode());
                    wo.setWoType(notice.getPickingType());
                    wo.setOriginNoticeNo(notice.getNoticeNo());//关联noticeno字段
                    JpaUtil.save(wo);
                }
                WarehouseOrderLine line = new WarehouseOrderLine();
                line.setWhCode(wo.getWhCode());
                line.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
                Item item = itemMap.get(vo.getItemCode());
                line.setItemCode(vo.getItemCode());
                if (item!=null){
                    line.setItemGroupCode(item.getItemGroupCode());
                    line.setItemName(item.getItemName());
                    line.setItemSpec(item.getItemSpec());
                    line.setUnitCode(item.getUnitCode());
                    line.setDrawingNo(item.getDrawingNo());
                }
                line.setQuantity(vo.getQuantity());
                line.setBizNo(wo.getBizNo());
                line.setLineNo(woLineNo+"");
                line.setOriginBizNo(vo.getBizOrderNo());
                line.setOriginBizLineNo(vo.getBizOrderlineNo());
                line.setSort(woLineNo+"");
                line.setLotNo(vo.getLotNo());
                line.setSubLotIndex(vo.getSubLotIndex());
                line.setOriginNoticeNo(vo.getNoticeNo());
                line.setOriginNoticeLineNo(vo.getNoticeLineNo());
                JpaUtil.save(line);
            }
            po.setOrderStatus(OrderStatusEnums.OrderStatus.PROCESSING.getStatusCode());
            po.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
            JpaUtil.update(po);
            return Result.OK("确认成功，已生成对应的通知和仓单",po);
        } else {
            return Result.error("非审核中状态不可审核");
        }
    }
}
